package com.yunxiao.cloud.job.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunxiao.cloud.job.entity.ScheduleJob;
import com.yunxiao.cloud.job.mapper.ScheduleJobMapper;
import com.yunxiao.cloud.job.service.ScheduleJobService;
import com.yunxiao.cloud.job.util.ScheduleUtil;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 定时任务 服务实现类
 * </p>
 *
 * @author yunxiao
 * @since 2020-01-09
 */
@Service
public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobMapper, ScheduleJob> implements ScheduleJobService {

    @Resource
    private Scheduler scheduler;

    @Resource
    private ScheduleJobMapper scheduleJobMapper;

    /**
     * 定时器初始化
     */
    @PostConstruct
    public void init() {
        ScheduleJob criterion = new ScheduleJob();
        List<ScheduleJob> scheduleJobList = scheduleJobMapper.selectByCondition(criterion);
        for (ScheduleJob scheduleJob : scheduleJobList) {
            CronTrigger cronTrigger = ScheduleUtil.getCronTrigger(scheduler, scheduleJob.getJobId());
            if (cronTrigger == null) {
                ScheduleUtil.createJob(scheduler, scheduleJob);
            } else {
                ScheduleUtil.updateJob(scheduler, scheduleJob);
            }
        }
    }

    @Override
    public ScheduleJob selectByJobId(String jobId) {
        return scheduleJobMapper.selectByJobId(jobId);
    }

    @Override
    public List<ScheduleJob> selectByCondition(ScheduleJob scheduleJob) {
        return scheduleJobMapper.selectByCondition(scheduleJob);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ScheduleJob record) {
        ScheduleJob scheduleJob = scheduleJobMapper.selectByJobId(record.getJobId());
        if (scheduleJob != null){
            return -1;
        }
        ScheduleUtil.createJob(scheduler, record);
        return scheduleJobMapper.insert(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(ScheduleJob record) {
        ScheduleUtil.updateJob(scheduler, record);
        return scheduleJobMapper.update(record, null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pauseJob(String jobId) {
        ScheduleJob scheduleJob = scheduleJobMapper.selectByJobId(jobId);
        ScheduleUtil.pauseJob(scheduler, jobId);
        scheduleJob.setStatus(1);
        scheduleJobMapper.update(scheduleJob, null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void resumeJob(String jobId) {
        ScheduleJob scheduleJob = scheduleJobMapper.selectByJobId(jobId);
        ScheduleUtil.resumeJob(scheduler, jobId);
        scheduleJob.setStatus(0);
        scheduleJobMapper.update(scheduleJob, null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(String jobId) {
        ScheduleJob scheduleJob = scheduleJobMapper.selectByJobId(jobId);
        ScheduleUtil.run(scheduler, scheduleJob);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String jobId) {
        ScheduleUtil.deleteJob(scheduler, jobId);
        scheduleJobMapper.deleteByJobId(jobId);
    }
}
